# 1.导入pytorch包
from pickle import LONG4

import torch



# 创建一个空的5*3张量
torch_empty = torch.empty((5, 3))
print(torch_empty)
# 3.创建一个随机初始化的5*3张量
print(torch.randint(0, 10, size=(5, 3)))  # randint随机生成整数张量
# 4.创建一个5*3的0张量,类型为long
print(torch.zeros(5, 3).type(torch.long))
# 5.直接从数组创建张量
import numpy as np


#  TODO 1.numpy数组转换为张量
# todo from_numpy()将numpy数组转换为张量,但是共享内存
# 创建numpy数组
n1 = np.array([1, 2, 3])
t1 = torch.from_numpy(n1)
print(n1, type(n1))
# 6.创建一个5*3的单位张量,类型为double
print(torch.ones(5, 3).type(torch.double))
data = torch.ones(5, 3).type(torch.double)

# 7.从已有的张量创建相同维度的新张量,并且重新定义类型为float
torch.rand_like(data).type(torch.float16)


# 8.打印一个张量的维度
print(data.shape)

# 9.将两个张量相加
randint1 = torch.randint(0, 10, size=(2, 2))
randint2 = torch.randint(0, 10, size=(2, 2))
print(randint1)
print(randint2)
print(torch.add(randint1, randint2))


# 10.取张量的第一列
randint2 = torch.randint(0, 10, size=(2, 2),dtype = torch.float32)

print(randint2[...,1])

# 11.将一个4*4的张量resize成一个一维张量
torch_randint = torch.randint(0, 10, size=(4, 4), dtype=torch.float32)
print(torch_randint.reshape(-1))

# 12 将一个4*4的张量,resize成一个2*8的张量
print(torch_randint.view(2,8))